package com.slg.android.manyanime;

import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;


public class IssueActivity extends FragmentActivity implements TextWatcher {
    @ViewInject(R.id.common_title_function_textView)
    private TextView send;
    @ViewInject(R.id.issue_content_textView)
    private EditText content;
    @ViewInject(R.id.issue_hint_textView)
    private TextView hint;
    private String temp;
    private int editStart ;
    private int editEnd ;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_issue);
        ViewUtils.inject(this);
        send.setVisibility(View.VISIBLE);
        send.setText("发送");
        content.addTextChangedListener(this);
    }

    @OnClick({R.id.common_title_function_textView, R.id.common_title_back_imageView})
    public void OnClick(View view) {
        int id = view.getId();
        switch (id) {
            case R.id.common_title_function_textView:

                finish();
                break;
            case R.id.common_title_back_imageView:
                finish();
                break;
        }

    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        temp = s.toString();
    }

    @Override
    public void onTextChanged(CharSequence s, int start, int before, int count) {
        int length = s.length();
        hint.setText("还可以输入"+(300-length)+"字");
    }

    @Override
    public void afterTextChanged(Editable s) {
        editEnd = content.getSelectionEnd();
        editStart = content.getSelectionStart();
        if(temp.length()>300){
            s.delete(editStart-1, editEnd);
            int tempSelection = editStart;
            content.setText(s);
            content.setSelection(tempSelection);
        }

    }
}
